package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    //本地注入
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    //查询数据库用户信息 以及他的角色的权限
    @Override
    public User findByUsername(String username) {
       User user= userDao.findByUsername(username);
       //判断 该用户 是否为空
       if(user==null){
           return  null;
       }
        Integer userId = user.getId();

   //根据用户Id查询角色
        Set<Role>roles= roleDao.findByUserId(userId);
        for (Role role : roles) {
            //获取角色Id
            Integer roleid = role.getId();
            Set<Permission> permissions = permissionDao.findByRoleId(roleid);
            //让角色关联权限
            role.setPermissions(permissions);
        }
        //让用户关联角色
        user.setRoles(roles);
        return user;
    }

    //分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //获取一页几行
        Integer pageSize = queryPageBean.getPageSize();
        //获取当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取查找的字符串
        String queryString = queryPageBean.getQueryString();
        //利用分页助手
        PageHelper.startPage(currentPage,pageSize);
        //调用dao层方法
        Page<User> page = userDao.findByCondition(queryString);
        long total = page.getTotal();
        List<User> rows = page.getResult();
        return new PageResult(total,rows);
    }

    //按ID查询
    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }
    //根据用户id查询角色id
    @Override
    public List<Integer> findRoleIdByUserId(Integer id) {
        return userDao.findRoleIdByUserId(id);
    }
    //新增
    @Override
    public void add(User user, Integer[] roles) {
        //先添加用户
       userDao.add(user);
        //根据反回来的Id建立关系
        Integer userId= user.getId();
        //调用方法建立关系
        this.setUserAndRole(userId,roles);
    }

    //编辑用户
    @Override
    public void edit(User user, Integer[] roles) {
        //修改用户
        userDao.edit(user);
        //获取用户Id
        Integer userId = user.getId();
        //取消关联关系
        userDao.upsetUserAndRole(userId);
        //建立关系表
        this.setUserAndRole(userId,roles);
    }

    //建立用户与与权限关系
    public void setUserAndRole(Integer userId,Integer[] roles){
        //获取检查项ID 需要遍历
        if(roles != null && roles.length > 0){
            for (Integer role : roles) {
                Map<String,Integer> map=new HashMap<>();
                map.put("userId",userId);
                map.put("roleId",role);
                userDao.setUserAndRole(map);
            }
        }
    }

    //删除
    public void delete(Integer userId){
         //取消关联关系
        userDao.upsetUserAndRole(userId);
        //删除角色
        userDao.delete(userId);
    }

}
